#! /bin/sh
### BEGIN INIT INFO
# Provides:          lesskeeper
# Required-Start:    $syslog $remote_fs
# Required-Stop:     $syslog $remote_fs
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: lesskeeper - Less Keeper
# Description:       lesskeeper - Less Keeper
### END INIT INFO


DAEMON=/opt/less/keeper/bin/lesskeeper
DAEMON_ARGS="--prefix=/opt/less/keeper"
NAME=lesskeeper
NAMESTORE=lesskeeper-store

set -e

case "$1" in
    start)
        echo -n "Starting $NAME ...\n" 
        
        if pidof $NAME; then
            echo -n "Started\n"
            exit 0
        fi
        
        if pidof $NAMESTORE; then
            kill -9 `pidof $NAMESTORE`
            sleep 1
        fi
        
        # Start lesskeeper!
        $DAEMON $DAEMON_ARGS > /dev/null 2>&1 &
        #start-stop-daemon --start --quiet --umask 007 --chuid lesskeeper:lesskeeper --exec $DAEMON -- $DAEMON_ARGS > /dev/null 2>&1 &
        sleep 2
        echo -n "$NAME PID: "
        if pidof $NAME; then
            echo -n "Started\n"
        else
            echo -n "Failed\n"
        fi
        ;;

    stop)
        
        echo -n "Stopping $NAME ...\n"
        echo -n "$NAME PID: "
        
        if pidof $NAME; then
            kill -9 `pidof $NAME`
            sleep 1
        fi
        
        if pidof $NAMESTORE; then
            kill -9 `pidof $NAMESTORE`
            sleep 1
        fi
        
        echo -n "\nStopped\n"
        ;;

    restart)
        ${0} stop
        ${0} start
        ;;

    status)
        echo -n "$NAME PID: "
        if pidof -s $NAME; then
            echo -n "\n$NAME is running\n"
        else
            echo -n "\n$NAME is not running\n"
        fi
        ;;

    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" >&2
        exit 1
        ;;
esac

exit 0
